package com.example.demo.service.impl;

import com.example.demo.service.AsyncService;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;

import java.time.Duration;
import java.time.LocalDateTime;
import java.time.LocalTime;

@Service
public class AsyncServiceImpl implements AsyncService {


    @Async
    @Override
    public void testAsyncZhuJie() {

        System.out.println("Service中Async当前线程名称："+Thread.currentThread().getName());
        LocalTime time1 = LocalDateTime.now().toLocalTime();
        System.out.println("Service中第一次调用时间："+ time1);
        try{
            Thread.sleep(3000);
        }catch (InterruptedException e){
            e.printStackTrace();
        }
        LocalTime time2 = LocalDateTime.now().toLocalTime();
        System.out.println("Service中第二次调用时间："+ time2);

        //得出两个时间差
        Duration duration = Duration.between(time1,time2);
        long timeC = duration.toMillis();
        System.out.println("Service中2号结果两者时间相差："+ (timeC/1000)+"秒");

    }
}
